package service.impl;

import dao.ChatDao;
import dao.impl.ChatDaoImpl;
import domian.Chat;
import service.ChatService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ChatServiceImpl implements ChatService {
    ChatDao chatDao = new ChatDaoImpl();

    @Override
    public Integer addChat(Chat chat) {
        Integer integer = chatDao.addChat(chat);
        return integer;
    }

    @Override
    public Map<String,List<String>> findAllChatBySendUserIdAndReceiptUser(Integer send, Integer receipt) {
        System.out.println(send+"------"+receipt);
        List<Chat> allChat = chatDao.findAllChat();
        Map<String,List<String>> map = new HashMap<>();
        List<String> sendList = new ArrayList<>();
        List<String> receiptList = new ArrayList<>();
        for (Chat chat : allChat) {
            if (chat.getSend_id()==send && chat.getReceipt_id()==receipt){
                sendList.add(chat.getContent()+"?"+chat.getSend_time());
            }
            if (chat.getSend_id()==receipt && chat.getReceipt_id()==send){
                receiptList.add(chat.getContent()+"?"+chat.getSend_time());
            }
        }
        map.put("sentList",sendList);
        map.put("receiptList",receiptList);
        return map;
    }

    @Override
    public Integer findChatSum(Integer id) {
        int a = 0;
        List<Chat> allChat = chatDao.findAllChat();
        for (Chat chat : allChat) {
            if (id==chat.getReceipt_id()){
                a++;
            }
        }
        return a;
    }

    @Override
    public List<Chat> findAllChat() {
        List<Chat> allChat = chatDao.findAllChat();
        return allChat;
    }
}
